Ext.define('crm.view.OpportunityList', {
    extend: 'Ext.Panel',
    xtype: 'opportunityList',
    requires: [
        'crm.model.Opportunity', 'crm.view.Opportunity'
    ],
    config: {
        layout: 'card',
        groupByDay: true,
        loadingText: '加载中...'
    },
    initialize: function () {
        this.callParent();
        this.searchstore = new Ext.data.Store({
            model: 'crm.model.Opportunity',
            autoLoad: true,
            sorters: '',
            proxy: {
                type: 'ajax',
                url:server+ '/ext/com.cinsea.mobile.ext.CrmAction?action=getopportunitys',
                reader: {
                    type: 'json',
                    totalProperty: 'totalCount',
                    rootProperty: 'result'
                }

            }
        })
        this.list = new Ext.List({
            itemTpl: '<span class="name">{customer}-{name}</span> ',
            loadingText: false,
            plugins: [
                {
                    xclass: 'Ext.plugin.ListPaging',
                    autoPaging: false,
                    loadMoreText: '更多',
                    noMoreRecordsText: ''
                },
                {
                    xclass: 'Ext.plugin.PullRefresh',
                    pullRefreshText: '下拉刷新...',
                    releaseRefreshText: '释放停止...',
                    loadingText: '加载中...',
                    snappingAnimationDuration: 10
                }
            ],
            store: this.searchstore
        });


        this.list.on('itemtap', this.onSelect, this);
        this.listpanel = new Ext.Panel({
            items: this.list,
            layout: 'fit'
        });


        this.listpanel.add({
            xtype: 'toolbar',
            docked: 'top',
            layout: { pack: 'left' },
            items: [
                {
                    ui: 'back',
                    text: '返回',
                    scope: this,
                    handler: function () {
                        this.getParent().setActiveItem(this.prevCard, {
                            type: 'slide',
                            reverse: true,
                            scope: this,
                            after: function () {
                                this.destroy();
                            }
                        });
                    }
                },
                { xtype: 'spacer' },
                {
                    xtype: 'selectfield',
                    width: 200,
                    defaultPhonePickerConfig:{doneButton: '确定',cancelButton: '取消'},
                    xtype: 'selectfield',
                    placeHolder: '成功率',
                    options: [
                        {text: '全部', value: ''},
                        {text: '100%：合同已进入审批流程', value: '402881e43d3a2b27013d43fd8de93525'},
                        {text: '90%：客户已同意确立合作', value: '402881e43d3a2b27013d43fc14763524'},
                        {text: '70%：决策人已支持我方', value: '402881e43d3a2b27013d43fbd3fe3523'},
                        {text: '50%：产品方案优势确立', value: '402881e43d3a2b27013d43fac4933521'},
                        {text: '30%：客户对晨熹有好印象', value: '402881e43d3a2b27013d43fa6f143520'},
                        {text: '10%：初步意向', value: '402881e43d3a2b27013d43fb56663522'}
                    ],

                    name: 'q',
                    listeners: {
                        change: this.onFieldChange,
                        scope: this
                    }
                }
            ]
        });

        // Take off the spinner
        //this.list.el.unmask();

        this.add(this.listpanel);

    },
    onFieldChange: function (comp, value) {
        this.searchstore.getProxy().setExtraParam('q', value);
        this.searchstore.load();
    },
    onSelect: function (v, i) {
        record = this.searchstore.getAt(i);
        if (record !== undefined) {
            if (record.data.datatype == '2') url =server+ "/mobile/wfprocess.do";
            else if (record.data.datatype == '3') url = server+"/mobile/document.do";
            else  url =server+ "/mobile/process.do";
            Ext.Ajax.request({
                scope: this,
                url: url + "?action=getformdata&processid=" + record.data.processid,
                success: function (res) {
                    obj = Ext.decode(res.responseText);
                    var sessionCard = new crm.view.Opportunity();
                    sessionCard.renderform(obj);
                    sessionCard.setPrevcard(this.listpanel);
                    sessionCard.setProcessid(record.data.processid);
                    this.setActiveItem(sessionCard, 'slide');
                }
            });

        }

    },
    setProcessid: function (processid) {
        this.processid = processid;
        this.searchstore.getProxy().setExtraParam('q', processid);
        this.searchstore.load();
    },
    setPrevcard: function (prevc) {
        this.prevCard = prevc;
    }

});
